﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace JGSY.LHBS
{
    public partial class Frmday : Form
    {
        public Frmday()
        {
            InitializeComponent();
        }
        //今日收入   支出   收支   最高日支出   最低日支出
        decimal toadayIn = 0.00M, todayEx = 0.00M, todayInEx = 0.00M, highestEx = 0.00M, lowestEx = 0.00M;
        //收支记录列表
        List<RecordInfo> recordList = new List<RecordInfo>();
        private void button3_Click(object sender, EventArgs e)
        {
            StatisticsData();
        }

        private void Frmday_Load(object sender, EventArgs e)
        {
            StatisticsData();
        }

        /// <summary>
        /// 加载与统计日收支
        /// </summary>
        private void StatisticsData()
        {
            listView1.Items.Clear();//清空统计列表
                                         //加载所有收支记录
            LoadRecordList();
            //自动统计
            StatisticsDayList();
            //加载统计数据
            LoadStatInfo();
        }

        /// <summary>
        /// 加载所有收支记录
        /// </summary>
        private void LoadRecordList()
        {
            //重置
            toadayIn = 0.00M;
            todayEx = 0.00M;
            todayInEx = 0.00M;
            highestEx = 0.00M;
            lowestEx = 0.00M;
            recordList = CommonHelper.GetAllRecordList();
        }

        /// <summary>
        /// 按日统计数据并加载
        /// </summary>
        private void StatisticsDayList()
        {
            //筛选日期列表
            List<string> listDate = recordList.Select(r => r.RecordTime.ToString("yyyy-MM-dd")).Distinct().ToList();
            //按日期列表统计处理--包含今日收支统计、最高/低日支出
            List<DayStatInfo> dayList = new List<DayStatInfo>();
            foreach (string date in listDate)
            {
                var list = recordList.Where(r => r.RecordTime.ToString("yyyy-MM-dd") == date);
                decimal inTotal = 0.00M, exTotal = 0.00M, inexTotal = 0.00M;
                //遍历筛选出来的记录列表
                foreach (var record in list)
                {
                    //总计 当前日期的收入和支出
                    if (record.ItemType == "收入")
                    {
                        inTotal += record.Money;
                    }
                    else
                    {
                        exTotal += record.Money;
                    }
                }
                inexTotal = inTotal - exTotal;//当前的收支数据
                                              //如果当前的date就是今日的日期，保存今日的数据
                if (date == DateTime.Today.ToString("yyyy-MM-dd"))
                {
                    toadayIn = inTotal;
                    todayEx = exTotal;
                    todayInEx = inexTotal;
                }
                //当前的收支情况添加到统计列表
                dayList.Add(new DayStatInfo()
                {
                    Date = date,
                    InExTotal = inexTotal,
                    InTotal = inTotal,
                    ExTotal = exTotal
                });
            }

            //加载统计列表
            if (dayList.Count > 0)
            {
                var exList = dayList.Select(d => d.ExTotal);
                highestEx = exList.Max();//最高日支出
                lowestEx = exList.Min();//最低日支出
                foreach (DayStatInfo item in dayList)
                {
                    ListViewItem li = new ListViewItem();
                    li.Text = item.Date;
                    li.SubItems.Add("￥" + item.InTotal.ToString("0.00"));
                    li.SubItems.Add("￥" + item.ExTotal.ToString("0.00"));
                    li.SubItems.Add("￥" + item.InExTotal.ToString("0.00"));
                    listView1.Items.Add(li);
                }
            }

        }

        /// <summary>
        /// 加载统计数据
        /// </summary>
        private void LoadStatInfo()
        {
            label2.Text = "￥" + toadayIn.ToString("0.00") + " 元";
            label5.Text = "￥" + todayEx.ToString("0.00") + " 元";
            label6.Text = "￥" + todayInEx.ToString("0.00") + " 元";
            label9.Text = "￥" + highestEx.ToString("0.00") + " 元";
            label11.Text = "￥" + lowestEx.ToString("0.00") + " 元";
        }
    }
}
